<template>
  <div class="login">
    <div class="l_con">
      <div class="l_top">
        <img src="../assets/logo.png" alt="" />
      </div>
      <div class="l_main">
        <!-- loginformref表单实例对象 -->
        <el-form :model="loginform" :rules="rules" ref="loginformref">
          <el-form-item prop="username">
            <el-input
              v-model="loginform.username"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginform.password"
              prefix-icon="el-icon-lock"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" :plain="true" @click="login">登录</el-button>
            <el-button type="info" :plain="true" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "app",
  data() {
    return {
      loginform: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 10,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    reset() {
      //重置
      console.log(this);
      this.$refs.loginformref.resetFields();
    },
    login() {
      //登陆验证
      this.$refs.loginformref.validate(async (va) => {
        if (!va) return;
        const { data: re } = await this.$http.post("login", this.loginform); //await 把函数修饰成异步的 函数前要加上async
        console.log(re);
        if (re.meta.status !== 200) {
          this.$message.error('登陆失败，用户不存在');
        } else {
          this.$message({
            message: "登录成功！",
            type: "success",
          });
          //登陆成功之后，token保存到客户端的sessionStorage中
          //   token只有在当前网站打开期间生效，所以保存在session Storage中
          window.sessionStorage.setItem('token',re.data.token);
          this.$router.push('/home')
        }
      });
    },
  },
};
</script>

<style scoped>
.login {
  width: 100%;
  height: 100%;
  background: #2b4b6b;
}
.l_con {
  width: 460px;
  height: 330px;
  background: #ffffff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10px;
}
.l_top {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 10px #ddd;
}
.l_top > img {
  border-radius: 50%;
  width: 100%;
  height: 100%;
  background: #eee;
}
.l_main {
  margin: 0 auto;
  width: 78%;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.btns {
  float: right;
}
</style>
